package UILayer;

import CtrLayer.StaffCtr;
import ModelLayer.CurrentAccount;
import ModelLayer.CustomerCollection;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class AccountDetails extends JInternalFrame
{
	private String[] column_names = {"Sale number", "Customer name", "With delivery", "Total cost" };
	private boolean is_my_account;
	private String account_id;
	private StaffCtr staff_ctr;
	private JTable order_table;
	private JTextField name_tf;
	private JTextField phone_tf;
	private JTextField postal_tf;
	private JTextField id_tf;
	private JTextField city_tf;
	private JTextField add_tf;
	private JTextField sal_tf;
	private JTextField user_tf;
	private JTextField pass_tf;
	private JTextField tot_sale_tf;
	private JComboBox comboBox;
	private JPanel panel_1;
	private JButton edit_btn;


	
	public AccountDetails(String i_account_id)
	{
		super("Account details", true, true, true);
		account_id = i_account_id;
		is_my_account = CurrentAccount.get_id().equals(account_id);
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refresh();
			}
		});
		
		setBounds(280, 100, 581, 415);
		getContentPane().setLayout(new BorderLayout(0, 0));
		staff_ctr = new StaffCtr();
		
		make_table();
		if(!(is_my_account))
		{
			make_btn();
		}
		else
		{
			make_ctr_btns();
		}
		make_fields();
		

	}
	
	private void make_btn()
	{
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JButton ok_btn = new JButton("Ok");
		ok_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } });
		panel_1.add(ok_btn);
	}
	
	private void make_ctr_btns()
	{
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_1.add(panel, BorderLayout.WEST);
		
		JButton details_btn = new JButton("Details");
		details_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { details(); } });
		panel.add(details_btn);
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.EAST);
		
		edit_btn = new JButton("Edit my account");
		edit_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { edit(); } });
		panel_2.add(edit_btn);
		
		JButton cancel_btn = new JButton("Cancel");
		cancel_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } });
		panel_2.add(cancel_btn);
	}
	
	private void make_fields()
	{
		
		
		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.EAST);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0};
		gbl_panel_1.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel_1.columnWeights = new double[]{1.0, 1.0};
		gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel_1.setBorder(new EmptyBorder(5, 2, 2, 7));
		panel_1.setLayout(gbl_panel_1);
		make_lbls();
		make_tfs();
	
	}
	
	/*private void make_edit()
	{
		JButton change_username_btn = new JButton("Change username");
		change_username_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton.gridwidth = 2;
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 11;
		panel_1.add(change_username_btn, gbc_btnNewButton);
		
		JButton btnChangePassword = new JButton("Change password");
		btnChangePassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
			}
		});
		GridBagConstraints gbc_btnChangePassword = new GridBagConstraints();
		gbc_btnChangePassword.gridwidth = 2;
		gbc_btnChangePassword.gridx = 0;
		gbc_btnChangePassword.gridy = 12;
		panel_1.add(btnChangePassword, gbc_btnChangePassword);
		
		edit_btn.setText("View");
		ok_btn.setEnabled(true));
		
	}*/
	
	
	
	private void make_lbls()
	{
		JLabel id_lbl = new JLabel("ID");
		GridBagConstraints gbc_id_lbl = new GridBagConstraints();
		gbc_id_lbl.anchor = GridBagConstraints.WEST;
		gbc_id_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_id_lbl.gridx = 0;
		gbc_id_lbl.gridy = 0;
		panel_1.add(id_lbl, gbc_id_lbl);
		
		JLabel name_lbl = new JLabel("Name");
		GridBagConstraints gbc_name_lbl = new GridBagConstraints();
		gbc_name_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_name_lbl.anchor = GridBagConstraints.WEST;
		gbc_name_lbl.gridx = 0;
		gbc_name_lbl.gridy = 1;
		panel_1.add(name_lbl, gbc_name_lbl);
		
		JLabel phone_lbl = new JLabel("Phone number");
		GridBagConstraints gbc_phone_lbl = new GridBagConstraints();
		gbc_phone_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_phone_lbl.anchor = GridBagConstraints.WEST;
		gbc_phone_lbl.gridx = 0;
		gbc_phone_lbl.gridy = 2;
		panel_1.add(phone_lbl, gbc_phone_lbl);
		
		JLabel add_lbl = new JLabel("Address");
		GridBagConstraints gbc_add_lbl = new GridBagConstraints();
		gbc_add_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_add_lbl.anchor = GridBagConstraints.WEST;
		gbc_add_lbl.gridx = 0;
		gbc_add_lbl.gridy = 3;
		panel_1.add(add_lbl, gbc_add_lbl);
		
		JLabel postal_lbl = new JLabel("Postal code");
		GridBagConstraints gbc_postal_lbl = new GridBagConstraints();
		gbc_postal_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_postal_lbl.anchor = GridBagConstraints.WEST;
		gbc_postal_lbl.gridx = 0;
		gbc_postal_lbl.gridy = 4;
		panel_1.add(postal_lbl, gbc_postal_lbl);
		
		JLabel city_lbl = new JLabel("City");
		GridBagConstraints gbc_city_lbl = new GridBagConstraints();
		gbc_city_lbl.anchor = GridBagConstraints.WEST;
		gbc_city_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_city_lbl.gridx = 0;
		gbc_city_lbl.gridy = 5;
		panel_1.add(city_lbl, gbc_city_lbl);
		
		JLabel sal_lbl = new JLabel("Salary");
		GridBagConstraints gbc_sal_lbl = new GridBagConstraints();
		gbc_sal_lbl.anchor = GridBagConstraints.WEST;
		gbc_sal_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_sal_lbl.gridx = 0;
		gbc_sal_lbl.gridy = 6;
		panel_1.add(sal_lbl, gbc_sal_lbl);
		
		JLabel function_lbl = new JLabel("Function");
		GridBagConstraints gbc_function_lbl = new GridBagConstraints();
		gbc_function_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_function_lbl.anchor = GridBagConstraints.WEST;
		gbc_function_lbl.gridx = 0;
		gbc_function_lbl.gridy = 7;
		panel_1.add(function_lbl, gbc_function_lbl);

		JLabel tot_sale_lbl = new JLabel("Total sales");
		GridBagConstraints gbc_tot_sale_lbl = new GridBagConstraints();
		gbc_tot_sale_lbl.anchor = GridBagConstraints.WEST;
		gbc_tot_sale_lbl.insets = new Insets(0, 0, 5, 5);
		gbc_tot_sale_lbl.gridx = 0;
		gbc_tot_sale_lbl.gridy = 8;
		panel_1.add(tot_sale_lbl, gbc_tot_sale_lbl);

		if(!(is_my_account))
		{
			JLabel user_lbl = new JLabel("Username");
			GridBagConstraints gbc_user_lbl = new GridBagConstraints();
			gbc_user_lbl.anchor = GridBagConstraints.WEST;
			gbc_user_lbl.insets = new Insets(0, 0, 5, 5);
			gbc_user_lbl.gridx = 0;
			gbc_user_lbl.gridy = 9;
			panel_1.add(user_lbl, gbc_user_lbl);
			
			JLabel pass_lbl = new JLabel("Password");
			GridBagConstraints gbc_pass_lbl = new GridBagConstraints();
			gbc_pass_lbl.anchor = GridBagConstraints.WEST;
			gbc_pass_lbl.insets = new Insets(0, 0, 5, 5);
			gbc_pass_lbl.gridx = 0;
			gbc_pass_lbl.gridy = 10;
			panel_1.add(pass_lbl, gbc_pass_lbl);
		}
	}
	
	private void make_tfs()
	{
		
		id_tf = new JTextField();
		id_tf.setEnabled(false);
		GridBagConstraints gbc_id_tf = new GridBagConstraints();
		gbc_id_tf.anchor = GridBagConstraints.NORTH;
		gbc_id_tf.insets = new Insets(0, 0, 5, 0);
		gbc_id_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_id_tf.gridx = 1;
		gbc_id_tf.gridy = 0;
		panel_1.add(id_tf, gbc_id_tf);
		id_tf.setColumns(10);
		
		name_tf = new JTextField();
		name_tf.setEnabled(false);
		GridBagConstraints gbc_name_tf = new GridBagConstraints();
		gbc_name_tf.insets = new Insets(0, 0, 5, 0);
		gbc_name_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_name_tf.gridx = 1;
		gbc_name_tf.gridy = 1;
		panel_1.add(name_tf, gbc_name_tf);
		name_tf.setColumns(10);
		
		phone_tf = new JTextField();
		phone_tf.setEnabled(false);
		GridBagConstraints gbc_phone_tf = new GridBagConstraints();
		gbc_phone_tf.insets = new Insets(0, 0, 5, 0);
		gbc_phone_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_phone_tf.gridx = 1;
		gbc_phone_tf.gridy = 2;
		panel_1.add(phone_tf, gbc_phone_tf);
		phone_tf.setColumns(10);
		
		add_tf = new JTextField();
		add_tf.setEnabled(false);
		GridBagConstraints gbc_add_tf = new GridBagConstraints();
		gbc_add_tf.insets = new Insets(0, 0, 5, 0);
		gbc_add_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_add_tf.gridx = 1;
		gbc_add_tf.gridy = 3;
		panel_1.add(add_tf, gbc_add_tf);
		add_tf.setColumns(10);
		
		postal_tf = new JTextField();
		postal_tf.setEnabled(false);
		GridBagConstraints gbc_postal_tf = new GridBagConstraints();
		gbc_postal_tf.insets = new Insets(0, 0, 5, 0);
		gbc_postal_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_postal_tf.gridx = 1;
		gbc_postal_tf.gridy = 4;
		panel_1.add(postal_tf, gbc_postal_tf);
		postal_tf.setColumns(10);
		
		city_tf = new JTextField();
		city_tf.setEnabled(false);
		GridBagConstraints gbc_city_tf = new GridBagConstraints();
		gbc_city_tf.insets = new Insets(0, 0, 5, 0);
		gbc_city_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_city_tf.gridx = 1;
		gbc_city_tf.gridy = 5;
		panel_1.add(city_tf, gbc_city_tf);
		city_tf.setColumns(10);
		
		sal_tf = new JTextField();
		sal_tf.setEnabled(false);
		GridBagConstraints gbc_sal_tf = new GridBagConstraints();
		gbc_sal_tf.insets = new Insets(0, 0, 5, 0);
		gbc_sal_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_sal_tf.gridx = 1;
		gbc_sal_tf.gridy = 6;
		panel_1.add(sal_tf, gbc_sal_tf);
		sal_tf.setColumns(10);
		
		comboBox = new JComboBox();
		comboBox.setEnabled(false);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"employee", "manager", "owner"}));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 7;
		panel_1.add(comboBox, gbc_comboBox);
		
		tot_sale_tf = new JTextField();
		tot_sale_tf.setEnabled(false);
		GridBagConstraints gbc_tot_sale_tf = new GridBagConstraints();
		gbc_tot_sale_tf.insets = new Insets(0, 0, 5, 0);
		gbc_tot_sale_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_tot_sale_tf.gridx = 1;
		gbc_tot_sale_tf.gridy = 8;
		panel_1.add(tot_sale_tf, gbc_tot_sale_tf);
		tot_sale_tf.setColumns(10);
		
		if(!(is_my_account))
		{
			user_tf = new JTextField();
			user_tf.setEnabled(false);
			GridBagConstraints gbc_user_tf = new GridBagConstraints();
			gbc_user_tf.insets = new Insets(0, 0, 5, 0);
			gbc_user_tf.fill = GridBagConstraints.HORIZONTAL;
			gbc_user_tf.gridx = 1;
			gbc_user_tf.gridy = 9;
			panel_1.add(user_tf, gbc_user_tf);
			user_tf.setColumns(10);
			
			pass_tf = new JTextField();
			pass_tf.setEnabled(false);
			GridBagConstraints gbc_pass_tf = new GridBagConstraints();
			gbc_pass_tf.insets = new Insets(0, 0, 5, 0);
			gbc_pass_tf.fill = GridBagConstraints.HORIZONTAL;
			gbc_pass_tf.gridx = 1;
			gbc_pass_tf.gridy = 10;
			panel_1.add(pass_tf, gbc_pass_tf);
			pass_tf.setColumns(10);
		}
		
		fill_tfs();
	}
	
	private void fill_tfs()
	{
		String[] fields = staff_ctr.get_account_fields(account_id);
		id_tf.setText(account_id);
		name_tf.setText(fields[0]);
		phone_tf.setText(fields[1]);
		add_tf.setText(fields[2]);
		postal_tf.setText(fields[3]);
		city_tf.setText(fields[4]);
		sal_tf.setText(fields[7]);
		comboBox.setSelectedItem(fields[8]);
		tot_sale_tf.setText(fields[9]);
		if(!(is_my_account))
		{
			user_tf.setText(fields[5]);
			pass_tf.setText(fields[6]);
		}
		
		
	}
	
	private void make_table()
	{
		order_table = new JTable();
		order_table.setFillsViewportHeight(true);
		fill_table();

		order_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		order_table.setAutoCreateRowSorter(true);
		order_table.setRowSelectionAllowed(true);
		order_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		order_table.getTableHeader().setReorderingAllowed(false);
		order_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		order_table.getColumnModel().getColumn(1).setPreferredWidth(120);
		order_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		order_table.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});

		JScrollPane scroll_pane = new JScrollPane(order_table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}
	
	private void fill_table()
	{
		String[] ids = staff_ctr.get_order_ids(account_id);
		
		Object[][] filling = new Object[ids.length][4];
		for (int i = 0; i < ids.length; i++)
		{
			filling[i] = CustomerCollection.get_any_order_data(ids[i]);

		}
		order_table.setModel(new MyTableModel(filling, column_names));
	}
	
	private void details()
	{
		int row_index = order_table.getSelectedRow();
		if (row_index != -1)
		{
			if(order_table.getModel().getValueAt(row_index, 0) != null)
			{
				OrderDetails od = new OrderDetails((String) order_table.getModel().getValueAt(row_index, 1), String.valueOf(order_table.getModel().getValueAt(row_index, 0)));
				getDesktopPane().add(od);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				od.setLocation(x, y);
				od.setVisible(true);
			}
		}
	}
	
	private void edit()
	{
		
				AccountManager p = new AccountManager(account_id);
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
		
	}
	
	private void refresh()
	{
		make_tfs();
	}
	


}
